package com.tico.editor.editors.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tico.common.service.impl.AbstractService;
import com.tico.common.utils.BaseQuery;
import com.tico.common.utils.JsonUtils;
import com.tico.common.utils.QueryResult;
import com.tico.editor.editors.domain.HttpEditor;
import com.tico.editor.editors.controller.websocket.HttpWebSocket;
import com.tico.editor.editors.domain.Kafka;
import com.tico.editor.editors.kafkaThread.ProducerKafka;
import com.tico.editor.editors.mapper.HttpEditorMapper;
import com.tico.editor.editors.postman.HttpClientPostMan;
import com.tico.editor.editors.service.HttpEditorService;
import com.tico.editor.editors.service.KafkaService;
import com.tico.editor.editors.service.PubilcDataService;
import org.apache.commons.collections.CollectionUtils;
import org.hibernate.internal.util.StringHelper;
import org.nutz.lang.segment.CharSegment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.Serializable;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HttpEditorServiceImpl extends AbstractService<HttpEditor> implements HttpEditorService {
    @Autowired
    HttpClientPostMan httpClientPostMan;
    @Autowired
    KafkaService kafkaService;
    @Autowired
    HttpEditorMapper httpEditorMapper;
    @Autowired
    PubilcDataService pubilcDataService;

    @Override
    public JSONObject postman(HttpEditor httpEditor) {
        Map<String,String> headerMap = new HashMap<>();
        String headers = httpEditor.getHeaders();
        String[] split = headers.split("\n");
        for(String str : split){
            String[] split1 = str.split(":");
            headerMap.put(split1[0],split1[1]);
        }
        JSONObject jsonObject=null;
        try {
            switch (httpEditor.getReqType()){
                case "post":
                    jsonObject = httpClientPostMan.doPost(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "get":
                    jsonObject = httpClientPostMan.doGet(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "put":
                    jsonObject = httpClientPostMan.doPut(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "delete":
                    jsonObject = httpClientPostMan.doDelete(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
            }
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    @Override
    public void postmanReal(HttpEditor httpEditor) {
        Map<String,String> headerMap = new HashMap<>();
        String headers = httpEditor.getHeaders();
        String[] split = headers.split("\n");
        for(String str : split){
            String[] split1 = str.split(":");
            headerMap.put(split1[0],split1[1]);
        }
        JSONObject jsonObject=null;
        try {
            switch (httpEditor.getReqType()){
                case "post":
                    jsonObject = httpClientPostMan.doPost(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "get":
                    jsonObject = httpClientPostMan.doGet(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "put":
                    jsonObject = httpClientPostMan.doPut(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
                case "delete":
                    jsonObject = httpClientPostMan.doDelete(httpEditor.getUrl(), httpEditor.getBody(), headerMap);
                    break;
            }
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String kafkaTopic = httpEditor.getKafkaTopic();
        HashMap<String,Object> param = new HashMap<>();
        param.put("status","true");
        param.put("type","set");
        List<Kafka> kafkas = kafkaService.findByNamedParamList(param);
        if(StringHelper.isNotEmpty(kafkaTopic)){
            if(kafkas.size()==0){
                return;
            }
            ProducerKafka producerKafka = new ProducerKafka(kafkas.get(0).getKafkaUrl());
            producerKafka.produce(kafkaTopic,JSON.toJSONString(jsonObject));
        }
    }

    @Override
    public int update(HttpEditor obj){
        return httpEditorMapper.update(obj);
    }

    @Override
    public HttpEditor findById(Serializable id) {
        return httpEditorMapper.findById(id);
    }

    @Override
    public int insertBatch(List<HttpEditor> httpEditors) {
        return httpEditorMapper.insertBatch(httpEditors);
    }

    @Override
    public int insert(HttpEditor httpEditor) {
        return httpEditorMapper.insert(httpEditor);
    }
    @Override
    public List<HttpEditor > findByNamedParamList(Map<String,Object> param){
        return httpEditorMapper.findByNamedParamList(param);
    }

    @Override
    public int delete(Serializable id) {
        return httpEditorMapper.delete(id);
    }

    @Override
    public int remove(Serializable id) {
        return httpEditorMapper.remove(id);
    }

    @Override
    public QueryResult<HttpEditor> findPageInfo(BaseQuery baseQuery) {
        QueryResult<HttpEditor> result = new QueryResult<HttpEditor>();
        result.setQuery(baseQuery);
        Map<String, Object> params = result.getQuery().build();
        Integer amount = this.httpEditorMapper.countPageInfo(params);
        result.setTotalRecord(amount);
        if (amount == 0) {
            return result;
        }
        List<HttpEditor> list = httpEditorMapper.findPageInfo(params);
        if (!CollectionUtils.isEmpty(list)) {
            result.setResultList(list);

        }
        return result;
    }
}
